﻿using PRO.Admin;
using PRO.Product;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Common;
using PRO.Entity;

namespace web.Manage.ProductClass
{
    public partial class edit : ManageBase
    {
        private int id = CVGRequest.GetQueryInt("id", 0);
        private int cid = CVGRequest.GetQueryInt("cid", 0);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            DataTable dt = ClassLib.GetOurClassTree();
            DropDownList1.Items.Clear();
            DropDownList1.Items.Add(new ListItem("顶级分类", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                    this.DropDownList1.Items.Add(new ListItem(HttpUtility.HtmlDecode(dt.Rows[i]["ClassName"].ToString()), dt.Rows[i]["id"].ToString()));
            }

            if (id > 0)
            {
                this.DropDownList1.Enabled = false;
                ClassInfo info = ClassLib.GetClassInfo(id);
                if (info != null && info.ID > 0)
                {
                    if (DropDownList1.Items.FindByValue(info.PID.ToString()) != null)
                        DropDownList1.Items.FindByValue(info.PID.ToString()).Selected = true;
                    this.txtTitle.Text = info.ClassName;
                    this.txtDes.Text = info.ClassDes;
                }

            }
            else
            {
                if (DropDownList1.Items.FindByValue(cid.ToString()) != null)
                    DropDownList1.Items.FindByValue(cid.ToString()).Selected = true;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.txtTitle.Text.IsNullOrWhiteSpace())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('必须输入分类名称！');</script>");
            }
            else
            {
                if (id == 0)
                {
                    ClassInfo info = new ClassInfo();
                    info.PID = this.DropDownList1.SelectedValue.ToInt(0);
                    info.ClassName = this.txtTitle.Text;
                    info.ClassDes = this.txtDes.Text;
                    if (ClassLib.AddClass(info))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('添加成功！');</script>");
                        Bind();
                        this.txtTitle.Text = "";
                        this.txtDes.Text = "";
                    }
                    else
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('添加失败！');</script>");
                }
                else
                {
                    ClassInfo info = new ClassInfo();
                    info.ID = id;
                    info.PID = this.DropDownList1.SelectedValue.ToInt(0);
                    info.ClassName = this.txtTitle.Text;
                    info.ClassDes = this.txtDes.Text;
                    if (ClassLib.EditClass(info))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('编辑成功！');</script>");
                        Bind();
                    }
                    else
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('编辑失败！');</script>");
                }
            }
        }
    }
}